package io.renren.modules.jsf.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.renren.modules.jsf.entity.CarEntity;
import io.renren.modules.jsf.entity.vo.CourseVo;
import io.renren.modules.jsf.service.CarService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.jsf.entity.SubjectEntity;
import io.renren.modules.jsf.service.SubjectService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.common.utils.ShiroUtils.getUserEntity;


/**
 * @author chen
 * @email sun@gmail.com
 * @date 2022-03-31 00:37:02
 */
@RestController
@RequestMapping("jsf/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    @Autowired private CarService carService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("jsf:subject:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = subjectService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list1")
    public R list1(@RequestParam Map<String, Object> params) {
        List<SubjectEntity> list = subjectService.selectAll();
        List<CourseVo> data = list.stream().map(t -> {
            CourseVo courseVo = new CourseVo();
            courseVo.setLabel(t.getSubname());
            courseVo.setValue(String.valueOf(t.getSubid()));
            return courseVo;
        }).collect(Collectors.toList());


        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{subid}")
    @RequiresPermissions("jsf:subject:info")
    public R info(@PathVariable("subid") Integer subid) {
        SubjectEntity subject = subjectService.getById(subid);

        return R.ok().put("subject", subject);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("jsf:subject:save")
    public R save(@RequestBody SubjectEntity subject) {
        subjectService.save(subject);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("jsf:subject:update")
    public R update(@RequestBody SubjectEntity subject) {
        subjectService.updateById(subject);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("jsf:subject:delete")
    public R delete(@RequestBody Integer[] subids) {
        subjectService.removeByIds(Arrays.asList(subids));

        return R.ok();
    }

    @RequestMapping("/addToCar")
    public R addToCar(@RequestBody Integer subId) {
        SysUserEntity userEntity = getUserEntity();
        CarEntity carEntity = new CarEntity();
        SubjectEntity subjectEntity = subjectService.getById(subId);
        carEntity.setSubName(subjectEntity.getSubname());
        carEntity.setUserName(userEntity.getUsername());
        carEntity.setPrice(subjectEntity.getSellingprice());
        carService.save(carEntity);

        return R.ok();
    }

}
